package com.cwvs.helper;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

/**
 * Url帮组类
 */
@Component
public class UrlHelper {
	/**
	 * image01地址 端口
	 * 
	 * @param relativeUrl
	 * @return
	 */
	public String img01UrlRootWithPort(String relativeUrl) {
		return pathCat(img01UrlRootWithPort, relativeUrl);
	}

	/**
	 * resource url地址,js,css,css中使用的image
	 * 
	 * @param relativeUrl
	 * @return
	 */
	public String resourceUrl(String relativeUrl) {
		return pathCat(resourceUrlRoot, relativeUrl);
	}

	/**
	 * 图片上传url地址
	 * 
	 * @param relativeUrl
	 * @return
	 */
	public String img01Url(String relativeUrl) {
		return pathCat(img01UrlRoot, relativeUrl);
	}

	/**
	 * 路径连接
	 * 
	 * @param root
	 * @param relativeUrl
	 * @return
	 */
	private String pathCat(String root, String relativeUrl) {
		if (!root.endsWith("/")) {
			root = root + "/";
		}
		if (StringUtils.isBlank(relativeUrl)) {
			relativeUrl = "";
		}
		if (relativeUrl.startsWith("/")) {
			relativeUrl = relativeUrl.substring(1);
		}

		StringBuilder ret = new StringBuilder();
		ret.append(root);
		ret.append(relativeUrl);
		return ret.toString();
	}

	/**
	 * 确保斜杠结尾 ^_^
	 * 
	 * @return
	 */
	// private String quebao_xiekang_jiewei(String path) {
	// if (StringUtils.isBlank(path)) {
	// return "/";
	// }
	// if (path.endsWith("/")) {
	// return path;
	// } else {
	// return path + "/";
	// }
	// }
	/**
	 * 资源库地址.
	 */
	private String resourceUrlRoot;
	/**
	 * 图片上传服务器地址(不带端口).
	 */
	private String img01UrlRoot;
	/**
	 * 图片上传服务器地址(带端口).
	 */
	private String img01UrlRootWithPort;
	private String videoUrlRoot;
	private String uploadFile;
	public String getResourceUrlRoot() {
		return resourceUrlRoot;
	}

	public void setResourceUrlRoot(String resourceUrlRoot) {
		this.resourceUrlRoot = resourceUrlRoot;
	}

	public String getImg01UrlRoot() {
		return img01UrlRoot;
	}

	public void setImg01UrlRoot(String img01UrlRoot) {
		this.img01UrlRoot = img01UrlRoot;
	}

	public String getImg01UrlRootWithPort() {
		return img01UrlRootWithPort;
	}

	public void setImg01UrlRootWithPort(String img01UrlRootWithPort) {
		this.img01UrlRootWithPort = img01UrlRootWithPort;
	}
	
	public String getUploadFile() {
		return uploadFile;
	}

	public void setUploadFile(String uploadFile) {
		this.uploadFile = uploadFile;
	}

	public String getVideoUrlRoot() {
		return videoUrlRoot;
	}

	public void setVideoUrlRoot(String videoUrlRoot) {
		this.videoUrlRoot = videoUrlRoot;
	}

	/**
	 * 替换图片地址
	 * 
	 * @return
	 */
	public  String replaceImage(String content) {
		if(StringUtils.isBlank(content)){
			return "";
		}
		return content.replace("/uploads/images",getImg01UrlRoot()).replace("&gt;", ">").replace("&lt;", "<");
	};

	/**
	 * 替换视频地址
	 * 
	 * @return
	 */
	public  String replaceVideo(String content) {
		if(StringUtils.isBlank(content)){
			return "";
		}
		return content.replace("/module/public",getVideoUrlRoot());
	};
}
